﻿(function (RMS) {
    var Control = RMS.BaseControl.prototype.extend(),
        defaults = {
            id: RMS.pub.randomGuid(),
            width: 100,
            height: 200,
            controlType: '1'
        },
        instance;

    Control.prototype.init = function (settings) {
        //default settings properties
        if (settings) {
            defaults = $.extend(true, defaults, settings);
        }
        if (defaults.id.indexOf('#') == -1 && defaults.id.indexOf('.') == -1) {
            defaults.id = '#' + defaults.id;
        }
        switch (defaults.controlType) {
            case '1':
                defaults.jqxType = 'jqxInput';
                break;

            default:
                defaults.jqxType = 'jqxInput';
                break;
        }
        console.log('::control::init::', defaults);
        var currSetting = $.extend(true, {}, defaults);
        //delete some properties recycle
        delete currSetting.controlType;
        delete currSetting.id;

        instance = $(defaults.id).factory(currSetting);
    };

    Control.prototype.getInstance = function (settings) {
        return instance;
    };

    Control.prototype.getSettings = function (settings) {
        return defaults;
    };

    Control.prototype.updateSettings = function (settings) {
        if (settings) {
            defaults = $.extend(true, defaults, settings);
        }
    };

    RMS.CControl = Control;
})(RMS);

//=====================================================================================================
//Overwrite jqWidget custom class
//=====================================================================================================
$.fn.factory = function (action) {
    var i
    , ilen
    , args
    , output
    , userSettings
    , $this = this
    , instance = $this.data('rms-ctrl');

    // Init case
    if (typeof action !== 'string') {
        userSettings = action || {};
        if (instance) {
            instance.updateSettings(userSettings);
        }
        else {
            instance = new jqxControl.Core($this, userSettings);
            $this.data('rms-ctrl', instance);
            instance.init();
        }
        return $this;
    }
    // Action case
    else {
        if (instance) {
            output = instance.execute.apply(instance, arguments);
        }
        return output;
    }
};
//=====================================================================================================
//JQWidgets custom class
//=====================================================================================================
var jqxControl = {
    Message: {
        NotSupport: 'jqWidgets do not provide action: '
    }
};
jqxControl.Core = function ($ctrl, settings) {
    this.defaults = {};
    this.jqxType = null;
    this.$ctrl = $ctrl;

    //extend properties
    $.extend(true, this.defaults, settings);

    //init function
    this.init = function () {
        this.jqxType = this.defaults.jqxType;
        if (typeof this.jqxType != 'undefined' && typeof this.$ctrl[this.jqxType] != 'undefined') {
            delete this.defaults.jqxType;
            this.$ctrl[this.jqxType](this.defaults);
        }
    };

    //update settings
    this.updateSettings = function (settings) {
        //extend properties
        $.extend(true, this.defaults, settings);
        this.$ctrl[this.jqxType](this.defaults);
    };

    this.getInstance = function () {
        return this;
    };
    this.execute = function () {
        var instance = this.$ctrl[this.jqxType].apply(this.$ctrl, ['getInstance']),
            action = arguments[0],
            args = [];
        if (arguments.length > 1) {
            for (i = 1, ilen = arguments.length; i < ilen; i++) {
                args.push(arguments[i]);
            }
        }

        if (typeof instance[action] != 'undefined') {
            return instance[action].apply(instance, args);
        }
        else {
            throw new Error(this.Message.NotSupport + action);
        }
    };
};
//=====================================================================================================
//jq UI, jeasy ui,.... custom class
//=====================================================================================================
//=====================================================================================================
//END
//=====================================================================================================     